//
//  MarketChildViewController.m
//  bitcoin
//
//  Created by  旺链 on 2018/5/23.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "MarketChildViewController.h"
#import "MarketTableViewCell.h"
#import "LeftMaketTableViewCell.h"
#import "MJRefresh.h"

@interface MarketChildViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)UITableView* table;
@property (nonatomic,strong)NSTimer *timer;

@end

@implementation MarketChildViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self getData];
    [self.timer setFireDate:[NSDate distantPast]];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.timer setFireDate:[NSDate distantFuture]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.table];
//    [self getData];
    
//    if (self.maketType == 100) {
//        //自选

//    }
    
}
- (NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(getData) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
    }
    return _timer;
}
- (void)getData
{
    NSString *url;
    if (self.maketType == 100) {
        //自选
        url = MARKET(@"myFav/");
    }else{
        url = [NSString stringWithFormat:@"%@/%ld",MARKET(@"listMarket"),(long)self.maketType];
    }
    
    [LSRequestManager getRequestWithURL:url params:nil success:^(NSDictionary *responseData) {
        
        [self.table.mj_header endRefreshing];
        HandleResponse(
            [self.dataArr removeAllObjects];
            if (![responseData[@"data"] isKindOfClass:[NSNull class]]) {

            [self.dataArr addObjectsFromArray:responseData[@"data"]];
            [self.table reloadData];
            })}
//        if (Success) {
//
//            if (![responseData[@"data"] isKindOfClass:[NSNull class]]) {
//
//                [self.dataArr addObjectsFromArray:responseData[@"data"]];
//                [self.table reloadData];
//            }
//
//        }else{
//            [MBProgressHUD showError:responseData[@"msg"]];
//        }
    } failure:^(NSError *error) {
        [self.table.mj_header endRefreshing];
    }];
}
- (void)requestForFav:(NSString*)relationId btn:(UIButton*)btn
{
    if (!IsLogin) {
        [MBProgressHUD showSuccess:@"请先登录"];
        return;
    }
    [LSRequestManager postRequestWithURL:MARKET(@"addFav/") params:@{@"relationId":relationId} success:^(NSDictionary *responseData) {
        
        
        HandleResponse(
                       if (self.maketType == 100) {
                           [self getData];
                       }
                       btn.selected = !btn.selected;
                       [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshMyFav" object:relationId];
                       )}
    } failure:^(NSError *error) {
        
    } showHUD:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (self.isLeft) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"LeftMaketTableViewCell"];
        LeftMaketTableViewCell *temcell = (LeftMaketTableViewCell*)cell;
        
        NSDictionary *dic = [self.dataArr objectAtIndex:indexPath.row];
        [temcell configData:dic];
        
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"MarketTableViewCell"];
        
        __weak MarketTableViewCell* temcell = (MarketTableViewCell*)cell;
        NSDictionary *dic = [self.dataArr objectAtIndex:indexPath.row];
        [temcell configData:dic];
        
        //加入自选
        temcell.favClick = ^{
            
            [self requestForFav:dic[@"currencyRelationId"] btn:temcell.favBtn];
        };
    }
    
    
    //设置选中背景色
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:@"#292D41"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isLeft) {
        
        if (self.clickBack) {
            NSDictionary *dic = [self.dataArr objectAtIndex:indexPath.row];
            self.clickBack(dic);
            
            NSString *url = [NSString stringWithFormat:@"defaultRelationId/%@",dic[@"currencyRelationId"]];
            [LSRequestManager getRequestWithURL:MARKET(url) params:nil success:^(NSDictionary *responseData) {
                
                HandleResponse(
                               
                               )}
             
             } failure:^(NSError *error) {
                 
             }];
        }
    }
}
- (UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64-49) style:UITableViewStylePlain];
        
        if (self.isLeft) {
            _table.frame = CGRectMake(0, 0, self.view.frame.size.width-50, self.view.frame.size.height - 64);
        }
        _table.backgroundColor = [UIColor colorWithHexString:@"#1C2035"];
        _table.delegate = self;
        _table.dataSource = self;
        _table.separatorStyle = UITableViewCellEditingStyleNone;
        
        if (self.isLeft) {
            
            [_table registerNib:[UINib nibWithNibName:@"LeftMaketTableViewCell" bundle:nil] forCellReuseIdentifier:@"LeftMaketTableViewCell"];
        }else{
            [_table registerNib:[UINib nibWithNibName:@"MarketTableViewCell" bundle:nil] forCellReuseIdentifier:@"MarketTableViewCell"];
        }
        
        
        _table.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self.dataArr removeAllObjects];
            [self getData];
        }];
    }
    return _table;
}
- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
